package com.mall.controller;


import com.mall.entity.Orderproduct;
import com.mall.entity.Orderreturn;
import com.mall.entity.Orders;
import com.mall.httpResult.HttpResult;
import com.mall.httpResult.ResultCodeEnum;
import com.mall.service.OrderproductService;
import com.mall.service.OrderreturnService;
import com.mall.service.OrdersService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fire
 * @since 2020-04-16
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrdersService ordersService;
    @Autowired
    private OrderreturnService orderreturnService;
    
    @Autowired
    private OrderproductService orderproductService;

        

    // huo
    @ApiOperation("管理员分页获取所有订单")
    @GetMapping("/getOrdersByPage")
    public HttpResult getOrdersByPage(
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
            @RequestParam(value = "status") int status) {
        List<Orders> list = ordersService.getOrdersByPage(pageNo, pageSize, status);
        return HttpResult.success(list);
    }
    
    // huo
    @ApiOperation("管理员修改订单状态")
    @PostMapping("updateOrder")
    public HttpResult updateOrder(@RequestBody Orders order) {
        ordersService.updateById(order);
        return HttpResult.success();
    }

    // huo
    @ApiOperation("管理员删除订单")
    @PostMapping("deleteOrder")
    public HttpResult deleteOrder(@RequestParam(value = "orderId") int orderId) {
        
        ordersService.deleteOrder(orderId);
        return HttpResult.success();
    }

    

    // huo
    @ApiOperation("管理员处理售后订单")
    @PostMapping("updateReturnOrders")
    public HttpResult updateReturnOrders(@RequestBody Orderreturn orderreturn) {
        
        orderreturnService.updateById(orderreturn);
        return HttpResult.success();
    }
    
    // huo
    @ApiOperation("管理员查询待处理的售后订单")
    @GetMapping("getPendingReturnOrders")
    public HttpResult getPendingReturnOrders() {
        List<Orderreturn> list = new ArrayList<>();
        try {
            list = orderreturnService.getPendingReturnOrders();
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR,e.getMessage());
        }
        return HttpResult.success(list);
    }
    // huo
    @ApiOperation("管理员查看商品评价信息")
    @GetMapping("getEvaluates")
    public HttpResult getEvaluates(
            @RequestParam(value = "orderId", defaultValue = "-1") int orderId,
            @RequestParam(value = "type", defaultValue = "0") int type) {
        
        try {
            return HttpResult.success(orderproductService.getEvaluates(orderId, type));
        } catch (Exception e){
        return HttpResult.failure(ResultCodeEnum.SERVER_ERROR,e.getMessage());
        }
        
       
    }
}

